using Godot;

public enum TileType
{
    None = 0,
    Red,
    Green,
    Blue,
    Yellow,
    Purple
}

public partial class Tile : Area2D
{
    [Signal] public delegate void TileSelectedEventHandler(Tile tile);

    [Export]
    public TileType Type
    {
        get => _type;
        set
        {
            _type = value;
            UpdateAppearance();
        }
    }

    private TileType _type = TileType.None;
    private Sprite2d _sprite;
    private CollisionShape2D _collisionShape;

    private bool _isSelected = false;
    public Vector2 GridPosition { get; set; }

    public override void _Ready()
    {
        _sprite = GetNode<Sprite2d>("Sprite2D");
        _collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");

        //Connect("input_event", this, nameof(OnInputEvent));
        Connect("input_event", Callable.From<Node, InputEvent, int>((node, input, shapeIndex) =>
        {
            //GD.Print("Tile Input Event Connect");
            OnInputEvent(node, input, shapeIndex);
        }));
        //EmitSignal("input_event", this, nameof(OnInputEvent));
        UpdateAppearance();
    }

    public override void _Process(double delta)
    {
        var isPress = Input.IsActionPressed("press");
        if (isPress)
        {
            var rect = GetViewportRect();
            var point = GetViewport().GetMousePosition();
            if (GetViewportRect().HasPoint(GetViewport().GetMousePosition()))
            {
                GD.Print("按下鼠标左键");
            }
        }

        //var strength = Input.GetActionStrength("press");
        //if (strength > 0.5f)
        //{
        //    //this.Position.
        //    //GD.Print("按下鼠标左键");

        //    //EmitSignal(nameof(TileSelected), this);
        //}
    }

    public override void _UnhandledInput(InputEvent @event)
    {
        if (@event is InputEventKey eventKey)
        {
            GD.Print($"Tile Unhandled Input {eventKey.Keycode}");
            if (eventKey.Pressed && eventKey.Keycode == Key.Left)
            {
            }
        }
    }

    private void UpdateAppearance()
    {
        if (_sprite == null) return;

        switch (Type)
        {
            case TileType.Red:
                _sprite.Texture = GD.Load<Texture2D>("res://Assets/red.png");
                break;

            case TileType.Green:
                _sprite.Texture = GD.Load<Texture2D>("res://Assets/green.png");
                break;

            case TileType.Blue:
                _sprite.Texture = GD.Load<Texture2D>("res://Assets/blue.png");
                break;

            case TileType.Yellow:
                _sprite.Texture = GD.Load<Texture2D>("res://Assets/yellow.png");
                break;

            case TileType.Purple:
                _sprite.Texture = GD.Load<Texture2D>("res://Assets/purple.png");
                break;

            default:
                _sprite.Texture = null;
                break;
        }
    }

    public void Select()
    {
        _isSelected = true;
        Modulate = new Color(1, 1, 1, 0.5f);
    }

    public void Deselect()
    {
        _isSelected = false;
        Modulate = new Color(1, 1, 1, 1);
    }

    private void OnInputEvent(Node viewport, InputEvent @event, int shapeIdx)
    {
        if (@event is InputEventMouseButton mouseEvent && mouseEvent.Pressed)
        {
            if (mouseEvent.ButtonIndex == MouseButton.Left)
            {
                GD.Print($"鼠标按下{viewport.GetViewport().GetMousePosition()}");
                EmitSignal(nameof(TileSelected), this);
            }
        }
    }
}